package com.realgodo.site.web.user.avatar;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springside.modules.web.struts2.Struts2Utils;

import com.realgodo.site.common.Constant;
import com.realgodo.site.common.FileUpload;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.web.user.UserDefaultController;
import com.realgodo.util.OperateImage;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/user/") })
public class IndexController extends UserDefaultController {

	private static final long serialVersionUID = 1109841568786460094L;

	private String avatar;

	private File file;

	@SuppressWarnings("unused")
	private String contentType;

	private String fileName;

	private String sourcepath;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

	private Date currentDate = Calendar.getInstance().getTime();

	public String execute() {
		avatar = super.getMySpace().getUser().getAvatar();
		return SUCCESS;
	}

	public String upload() {
		if (null != fileName) {
			String filename = FileUpload.getFileName(fileName);
			String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
					+ "/" + filename;
			String filepath = getRequest().getSession().getServletContext()
					.getRealPath(temp);
			FileUpload.saveFile(filepath, file);
			this.sourcepath = Constant.ROOT_PATH + temp;
		}
		return "upload";
	}

	/**
	 * 剪切左上点坐标 x
	 */
	private int labelX;
	/**
	 * 剪切左上点坐标 y
	 */
	private int labelY;

	/**
	 * 剪切点宽度
	 */
	private int width;
	/**
	 * 剪切点高度
	 */
	private int height;

	public String save() {
		sourcepath = sourcepath.replace("meimei", "");
		sourcepath = sourcepath.replace("/", "\\");
		OperateImage o = new OperateImage(labelX, labelY, width, height);
		sourcepath = Struts2Utils.getRequest().getSession().getServletContext()
				.getRealPath(sourcepath);
		// 目标文件

		String filename = FileUpload.getFileName(sourcepath);
		String temp = Constant.UPLOAD_IMAGE_PATH + sdf.format(currentDate)
				+ "/" + filename;
		String newSourcepath = Struts2Utils.getRequest().getSession()
				.getServletContext().getRealPath(temp);

		try {
			o.cut(sourcepath, newSourcepath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		/**
		 * 剪裁后网络地址
		 */
		newSourcepath = Constant.ROOT_PATH + temp;
		User user = super.getMySpace().getUser();
		user.setAvatar(newSourcepath);
		this.userManager.save(user);
		return "toindex";
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public void setUpload(File file) {
		this.file = file;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSourcepath() {
		return sourcepath;
	}

	public void setSourcepath(String sourcepath) {
		this.sourcepath = sourcepath;
	}

	public String getFileName() {
		return fileName;
	}

	public int getLabelX() {
		return labelX;
	}

	public void setLabelX(int labelX) {
		this.labelX = labelX;
	}

	public int getLabelY() {
		return labelY;
	}

	public void setLabelY(int labelY) {
		this.labelY = labelY;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
}
